package com.hb.trade.server.project.file.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.file.entity.BidBuyRecord;
import com.hb.core.pojo.project.section.dto.BidDownRecordDTO;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.file.service.BidBuyRecordService;
import com.hb.trade.server.project.proj.service.ProjectService;
import com.hb.trade.server.project.section.service.raw.BidDownRecordService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import io.swagger.annotations.Api;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 供应商购标记录表 前端控制器
 *
 * @author zhaojk
 * @since 2021-09-16
 */
@Api(tags = "文件售卖记录(领购记录)", value = "文件售卖记录(领购记录)")
@RestController
public class BidBuyRecordController {

    protected final ProjectService projectService;
    protected final BidSectionService bidSectionService;
    protected final BidBuyRecordService bidBuyRecordService;
    protected final BidDownRecordService bidDownRecordService;

    public BidBuyRecordController(ProjectService projectService,
                                  BidSectionService bidSectionService,
                                  BidBuyRecordService bidBuyRecordService,
                                  BidDownRecordService bidDownRecordService) {
        this.projectService = projectService;
        this.bidSectionService = bidSectionService;
        this.bidBuyRecordService = bidBuyRecordService;
        this.bidDownRecordService = bidDownRecordService;
    }

    /**
     * 售卖记录
     *
     * @param sectionDto
     * @return
     */
    @PostMapping("/bidBuyRecord/saleRecord")
    public Message saleRecord(@RequestBody BidSectionDTO sectionDto) {
        Page<JSONObject> pages = sectionDto.pages();
        bidDownRecordService.pageDownloadRecords(pages, sectionDto.getSectionId());
        for (JSONObject record : pages.getRecords()) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            record.putOpt("downTime", df.format(record.getDate("downTime")).toString());
        }
        return MsgUtil.success(PageResult.get(pages));
    }


    /**
     * 查询标段的下载记录
     *
     * @param sectionDto
     * @return
     */
    @PostMapping("/bidBuyRecord/listDownloadBySectionId")
    public Message saleRecordList(@RequestBody BidSectionDTO sectionDto) {
        List<JSONObject> bidDownRecordDTOS = bidDownRecordService.listDownloadBySectionId(sectionDto.getSectionId());
        return MsgUtil.success(bidDownRecordDTOS);
    }


    /**
     * 投标人支付成功保存供应商购标记录
     *
     * @param bidBuyRecord 购标记录
     * @return
     */
    @PutMapping("/bidBuyRecord/saveRecord")
    public Message<Boolean> saveRecord(@RequestBody BidBuyRecord bidBuyRecord) {
        if (StrUtil.isEmpty(bidBuyRecord.getOrderNo())) {
            return MsgUtil.fail("订单号不可为空");
        }
        BidBuyRecord bidBuyRecordDb = bidBuyRecordService.getBidBuyRecordByOrgId(
                bidBuyRecord.getSectionId()
                , bidBuyRecord.getBuyUserId(),
                bidBuyRecord.getOrgId()
        );
        if (bidBuyRecordDb != null) {
            bidBuyRecord.to(bidBuyRecordDb);
//            return MsgUtil.success(bidBuyRecordService.updateById(bidBuyRecordDb));
            return MsgUtil.success(true);
        }
        //查询招标文件id
        BidSection bidSection = bidSectionService.getById(bidBuyRecord.getSectionId());
        if (bidSection != null){
            bidBuyRecord.setFileId(bidSection.getFileId());
        }
        bidBuyRecord.setDownloadFlag(LogicEnum.NO.getCode());
        return MsgUtil.success(bidBuyRecordService.save(bidBuyRecord));

    }

    /**
    * Description: 判断当前企业是否已购买标书
    * @author: fengbin
    * @date: 2022/2/28 15:27
    * @param bidBuyRecord
     @return: org.fzzn.core.model.msg.Message<java.lang.Boolean>
    */
    @PostMapping("/bidBuyRecord/judgeCompanyRecord")
    public Message<Boolean> judgeCompanyRecord(@RequestBody BidBuyRecord bidBuyRecord) {
        //获取当前企业购标信息
        BidBuyRecord bidBuyRecordDb = bidBuyRecordService.getBidBuyRecordByOrgId(
                bidBuyRecord.getSectionId()
                , null,
                bidBuyRecord.getOrgId()
        );
        if (bidBuyRecordDb != null && !bidBuyRecord.getBuyUserId().equals(bidBuyRecordDb.getBuyUserId()) ) {
            return MsgUtil.success(false);
        }
        return MsgUtil.success(true);
    }


    /**
     * 投标人下载成功之后更新领购信息
     *
     * @param bidBuyRecord 购标记录
     * @return
     */
    @PostMapping("/bidBuyRecord/updateRecord")
    public Message updateRecord(@RequestBody BidBuyRecord bidBuyRecord) {
        if (StrUtil.isEmpty(bidBuyRecord.getOrderNo())) {
            return MsgUtil.fail("订单号不可为空");
        }
        BidBuyRecord bidBuyRecordUpdate = bidBuyRecordService.findBidBuyRecordByOrderNo(bidBuyRecord.getOrderNo());
        if (bidBuyRecordUpdate == null) {
            return MsgUtil.success();
        }
        bidBuyRecordUpdate.setDownloadFlag(LogicEnum.YES.getCode());
        bidBuyRecordUpdate.setDownTime(LocalDateTime.now());
        bidBuyRecordService.updateById(bidBuyRecordUpdate);
        return MsgUtil.success();
    }


    /**
     * 根据多标段查询采购信息
     *
     * @author yuezheng
     * @param sectionIdList
     * @return org.fzzn.core.model.msg.Message
     * @date 2022/4/16 4:18 PM
     */
    @PostMapping("/bidBuyRecord/findBySectionIdList")
    public Message<List<BidBuyRecord>> findBuyRecordBySectionIdList(@RequestBody List<Long> sectionIdList) {
        QueryWrapper<BidBuyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(BidBuyRecord::getId, BidBuyRecord::getSectionId).in(BidBuyRecord::getSectionId,sectionIdList);
        List<BidBuyRecord> list = this.bidBuyRecordService.list(queryWrapper);
        return MsgUtil.success(list);
    }
}
